package org.benjamin.wechat.controller;

import java.util.List;

import javax.validation.Valid;

import org.benjamin.wechat.domain.entity.Account;
import org.benjamin.wechat.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(value = "accountManager")
public class AccountManagerController {
	
	@Autowired
	private AccountService accountService;
	
	@RequestMapping(value = "index")
	public ModelAndView index() {
		ModelAndView modelAndView = new ModelAndView("accountManager.index");
		List<Account> accounts = this.accountService.findAll();
		modelAndView.addObject("accounts",accounts);
		return modelAndView;
	}
	
	@RequestMapping(value = "to_add")
	public ModelAndView toAdd() {
		ModelAndView modelAndView = new ModelAndView("accountManager.add");
		modelAndView.addObject("account",new Account());
		return modelAndView;
	}
	
	@RequestMapping(value = "do_add")
	public String doAdd(@Valid Account account,BindingResult bindingResult) {
		if(bindingResult.hasErrors()) {
			return "accountManager.add";
		}
		this.accountService.submitInfo(account);
		return "accountManager.index";
	}

}
